import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @Author: WangYongGan
 * @Date: 2021/11/16 2:09
 * @Description:
 */

public class 双重for {
    public static int big = (int) Math.pow(10, 5);//1个亿
    public static int lit = 100;
    public static List<Integer> integerList = new ArrayList<>();


    public static void main(String[] args) {
        List<Consumer2<Consumer,Object>> testFunction = new ArrayList<>();
        Consumer2<Consumer, Object> smallBig = 双重for::smallBig;
        testFunction.add(双重for::smallBig);
        testFunction.add(双重for::bigSmall);
        Consumer<Integer> test = 双重for::test;
        Consumer<Integer> testCode = 双重for::action;
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.printf("-------------第%d次-------------\n", i + 1);
            int anInt = random.nextInt(2);
            getTime(testFunction.get(anInt), testCode,i);
            getTime(testFunction.get(1 - anInt), testCode,i);
            integerList.clear();
        }
    }

    private static void bigSmall(Consumer consumer, Object param) {
        for (int i = 0; i < big; i++) {
            for (int j = 0; j < lit; j++) {
                consumer.accept(param);
            }
        }
        System.out.print("外大内小");
    }
private static void test(int age){

}
    private static void smallBig(Consumer consumer, Object param) {
        for (int i = 0; i < lit; i++) {
            for (int j = 0; j < big; j++) {
                consumer.accept(param);
            }
        }
        System.out.print("外小内大");
    }
/**
 * @Description:
 * @Params:  bigOrLit：big/lit    testCode:测试代码
 * @return:
 **/
    private static void getTime( Consumer2 bigOrLit,Consumer testCode,Object param) {
        Date begin = new Date();
        bigOrLit.accept2(testCode,param);
        Date end = new Date();
        System.out.println("耗时: " + (end.getTime() - begin.getTime()) + "ms");
    }

    private static void action(int n) {
        integerList.add(n);
    }
}
@FunctionalInterface
interface Consumer2<T,R>{
   void accept2(T t,R r);
}
